package pro5_decoder_encoder.ex3_objectSerialize;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;


public class SerializationServerInitializer extends ChannelInitializer<Channel> {

    private final static int MAX_OBJECT_SIZE = 1024 * 1024; //
    private EventLoopGroup businessGroup;

    public SerializationServerInitializer(EventLoopGroup businessGroup) {
        this.businessGroup=businessGroup;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //接到对象流后，解码成java对象
        pipeline.addLast(new ObjectDecoder(MAX_OBJECT_SIZE,
                ClassResolvers.weakCachingConcurrentResolver(this.getClass()
                        .getClassLoader())));

        pipeline.addLast(new ObjectEncoder());

        pipeline.addLast(businessGroup, new SerializationServerHandler());
    }
}
